import React, { useState, useEffect } from 'react';
import services from '../../../services/api';
import { MODULE_TYPE, IS_EXEC_CONDITION, OPER_TYPE } from '../../../utils/constant';
import NoData from './noData';
import PolicyAdd from '../../Strategy/Strategy/add';
import StrategyComponentAdd from '../../Strategy/Strategy/Component/add';
import EventAdd from '../../Strategy/Event/add';
import EventScript from '../../Strategy/Event/script';
import FieldAdd from '../../Variable/Field/add';
import IndicatorAdd from '../../Variable/Indicator/add';
import RiskTypeAdd from '../../Variable/RiskType/add';
import NameListDetail from '../../Variable/NameList/Detail/index';
import NameListEdit from '../../Variable/NameList/Edit/index';
import MapCompare from '../../Variable/Map/compare';
import ModelCompare from '../../Variable/Model/Model/compare';
import FnAdd from '../../Variable/Fn/add';
import CustomParamsAdd from '../../Variable/Map/CustomParams/add';
import IndexDataSource from '../../Variable/Indicator/DataSource/add';

export default function AuditCompare(props) {
  const [afterContent, setAfterContent] = useState({});
  const [moduleType, setModuleType] = useState(0);
  const operType = props.oper_type;
  const [operType2, setOperType2] = useState(1);

  useEffect(() => {
    getDiffContent(props.record);
  }, []);

  function getDiffContent({ audit_id }) {
    services.audit.getDiffContent({ audit_id }).then((res) => {
      if (res.code === 0) {
        const after_content = res.data.after_content;
        // 如果是策略组件，需要处理下数据
        if (res.data.module_type === MODULE_TYPE._STRATEGY_COMPONENT) {
          if (after_content instanceof Object) {
            after_content.risk_type_f = JSON.stringify(after_content.risk_type);
            after_content.is_exec_condition =
              after_content &&
              after_content.strategy_config &&
              after_content.strategy_config.condition_groups &&
              after_content.strategy_config.condition_groups.length > 0
                ? IS_EXEC_CONDITION._TRUE
                : IS_EXEC_CONDITION._FALSE;
          }
        }
        setAfterContent(after_content);
        setModuleType(res.data.module_type);
        // setOperType(res.data.oper_type);
        setOperType2(res.data.oper_type2); //oper_type2字段的定义主要有：1基础数据 2脚本数据 3明细数据（例如匹配名单，第三方名单需要）
      }
    });
  }

  function getCompareComponent(type, operType2) {
    switch (type) {
      case MODULE_TYPE._RISK_TYPE: // 风险类型
        return RiskTypeAdd;
      case MODULE_TYPE._NAMELIST: // 名单
        if (props.actionType === 'detail') {
          return NameListDetail
        } else {
          return NameListEdit
        }
      case MODULE_TYPE._THIRD_NAMELIST: // 三方名单
        return MapCompare;
      case MODULE_TYPE._EVENT: // 事件
        // 2脚本数据
        if (operType2 === 2) {
          return EventScript;
        } else {
          return EventAdd;
        }

      case MODULE_TYPE._FIELD: // 字段
        return FieldAdd;
      case MODULE_TYPE._STRATEGY: // 策略
        return PolicyAdd;
      case MODULE_TYPE._MODEL: //模型
        return ModelCompare;
      case MODULE_TYPE._FEATURE: //指标
        return IndicatorAdd;
      case MODULE_TYPE._ACTION: // 函数
        return FnAdd;
      case MODULE_TYPE._INDEX_DATASOURCE: // 指标数据源
        return IndexDataSource;
      case MODULE_TYPE._STRATEGY_COMPONENT: // 策略组件
        return StrategyComponentAdd;
      case MODULE_TYPE._CUSTOM_PARAMS: // 自定义出参
        return CustomParamsAdd;
      case MODULE_TYPE._MAP: // 映射
        return MapCompare;
      default:
        return NoData;
    }
  }
  const Com = getCompareComponent(moduleType, operType2);

  return (
    <div>
      <div
        className={moduleType === MODULE_TYPE._STRATEGY ? 'stretegy-content' : null}
        style={moduleType === MODULE_TYPE._STRATEGY ? { marginBottom: '30px' } : null}
      >  
        <Com
          {...props}
          value={afterContent || {}}
          record={afterContent || {}}
          actionType={props.actionType}
          isImport={operType === OPER_TYPE._INPORT ? true : false}
          isAuditLog={true}
          isImportant={operType === OPER_TYPE._INPORT ? true : false}
          actions={(afterContent || {}).actions}
        />
      </div>
    </div>
  );
}
